package org.two;

import java.io.*;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
public class TestObjectStream {
    public static void main(String[] args) {
        String dir = System.getProperty("user.dir");
        String s = File.separator;

        Users user = new Users(1, "张三三", "123456");   // 创建一个对象

        try (
                ObjectOutputStream oos =
                        new ObjectOutputStream(
                                new FileOutputStream(dir + s + "obj.obj")
                        );) {
            oos.writeObject(user);      // 将对象写入到文件中
            System.out.println("obj序列化成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (
                ObjectInputStream ois =
                        new ObjectInputStream(new FileInputStream(dir + s + "obj.obj"));
        ) {
                Users u = (Users)ois.readObject();
            System.out.println(u.getId());
            System.out.println(u.getName());
            System.out.println(u.getPwd());
            System.out.println("反序列化完成");
        }catch( IOException | ClassNotFoundException e ) {
            e.printStackTrace();
        }
    }
}

class Users implements Serializable{
    private int id;
    private String name;
    private String pwd;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Users(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public Users() {
    }
}
